<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        $prefix = request()->route()->getPrefix();
        if ($prefix=='admin'&&!$request->expectsJson()) {
            return route('admin.auth.login');
        }elseif ($prefix=='api/v1'&&!$request->expectsJson()){
//            if (!auth('sanctum')->check()){
//                admin_exit(response(null,401));
//            }
        }
    }

    protected function authenticate($request, array $guards)
    {
        if (empty($guards)) {
            $guards = [null];
        }

        foreach ($guards as $guard) {
            if ($this->auth->guard($guard)->check()) {
                return $this->auth->shouldUse($guard);
            }
        }

        $this->unauthenticated($request, $guards);
        return parent::authenticate($request, $guards); // TODO: Change the autogenerated stub
    }
}
